package com.youpingou.controller;

import com.youpingou.domain.Cart;
import com.youpingou.service.CartService;
import com.youpingou.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @auther Created by yuanxinqi
 */
@RestController
@RequestMapping
public class CartController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private CartService cartService;

    @PostMapping("/carts")
    public BaseResult addToCart(@RequestBody Cart cartRequest){
        //1. 直接调用service层处理业务
        cartService.addToCart(cartRequest,request);
        //2. 准备返回数据
        return BaseResult.ok();
    }
    /**
     * 需要的参数：
     * 1 userId/token
     * 2 cart数据
     * @return
     */
    @GetMapping("/addLoginCart")
    public BaseResult addLoginCart(@RequestParam("userId") Integer userId, @RequestParam("carts") String carts){
        cartService.addLoginCart(userId,carts);
        //2. 返回结果
        return BaseResult.ok();
    }

    @GetMapping("/carts")
    public BaseResult findCartByToken(){
        List<Cart> list = cartService.findCartByToken(request);
        return BaseResult.ok(list);
    }

    @PutMapping("/carts/{skuid}")
    public BaseResult updateCart(@PathVariable Integer skuid,@RequestBody Cart cartReq){
        cartService.updateCart(skuid,cartReq,request);
        return BaseResult.ok();
    }


    @DeleteMapping("/carts/{skuid}")
    public BaseResult deleteCart(@PathVariable("skuid") Integer skuid){
        cartService.deleteCart(skuid,request);
        return BaseResult.ok();
    }

}